<?php

namespace app\admin\controller;
use app\BaseController;
use think\facade\Db;

class Order extends BaseController {

  public function list() {
    $request = request()->param();

    //分页查询
    $page = pageQuery($request);

    $status = isset($request['status'])?$request['status']:2;

    $list = Db::table('shop_order')->order('id', 'desc')->where('status', $status)->page($page['page'], $page['count'])->select();
    if (!$list) {
      returnJson(-2, '查询失败！');
    }
    $count = Db::table('shop_order')->where('status', $status)->count(); //总条数

    // 统计
    $waitDeliverCount = Db::table('shop_order')->where('status', 2)->count();
    $deliverCount = Db::table('shop_order')->where('status', 3)->count();
    
    $data = [
      'count' => $page['count'],
      'total' => $count,
      'rows' => $list,
      'badge' => [
        'status2' => $waitDeliverCount,
        'status3' => $deliverCount,
      ]
    ];
    
    returnJson(0, '成功！', $data);
  }

  public function order_goods_list() {
    $order_id = request()->param('order_id');

    $list = Db::table('shop_order_goods')->where('order_id', $order_id)->select();
    if (!$list) {
      returnJson(-2, '查询失败！');
    }
    
    returnJson(0, '成功！', $list);
  }


  public function deliver() {
    $request = request()->param();
    if ( !isset($request['id']) ) {
      returnJson(-2, '参数错误');
    }

    $id = $request['id'];
    $data = [
      'expressage_order_id' => request()->param('expressage_order_id'),
      'expressage_name' => request()->param('expressage_name'),
      'status' => 3
    ];
    
    $update = Db::table('shop_order')->where('id', $id)->update($data);
    if ($update >= 0) {
      returnJson(0, '发货成功！');
    }
    returnJson(-2, '发货失败，请刷新重试！');
  }



}